###################################################################################################
# Create Panel Means ##############################################################################
# Michael Olson and Albert Rivero #################################################################
###################################################################################################

###################################################################################################
# Packages and Working Directory ##################################################################
###################################################################################################

library(foreign)
library(tidyverse)

setwd("C:/Users/michael.p.olson/Dropbox/Research/Appellate court response to Supreme Court/full_replication_package")

circuits <- c("First",
              "Second",
              "Third",
              "Fourth",
              "Fifth",
              "Sixth",
              "Seventh",
              "Eighth",
              "Ninth",
              "Tenth",
              "Eleventh",
              "D.C.",
              "Federal")

###################################################################################################
# Load JCS Data Split out by Circuit and Month ####################################################
###################################################################################################
  
  jcs_circuit_month <- read_csv("jcs_circuit_month.csv")

my_tibble <- tibble(circuit = rep(circuits, times = length(1965:2017), each = 12),
                    year = rep(1965:2017, each = 13 * 12),
                    month = rep(1:12, times = 13 * length(1965:2017)))

my_tibble <- my_tibble %>%
  filter(! ((circuit == "Eleventh" & year < 1981) | (circuit == "Eleventh" & year == 1981 & month < 10)),
         ! ((circuit == "Federal" & year < 1982) | (circuit == "Federal" & year == 1982 & month < 10)))

mean_panel <- rep(NA, nrow(my_tibble))
mean_all <- rep(NA, nrow(my_tibble))
median_all <- rep(NA, nrow(my_tibble))

for(i in 1:length(mean_panel)) {
  i_year <- my_tibble$year[i]
  i_circuit <- my_tibble$circuit[i]
  i_month <- my_tibble$month[i]
  
  # We'll grab a) all judges who arrive STRICTLY before i_year and depart after
  # i_year, then b) for judges who arrive in i_year, those who arrive on/after
  # the current month, and c) for judges who depart in i_year, those who depart
  # on/before the current month
  
  i_jcs_a <- jcs_circuit_month %>%
    filter(yeara < i_year, yearl > i_year, circuit == i_circuit) %>%
    pull(JCS)
  
  i_jcs_b <- jcs_circuit_month %>%
    filter(yeara == i_year, mona <= i_month, circuit == i_circuit, yeara!=yearl) %>%
    pull(JCS)

  i_jcs_c <- jcs_circuit_month %>%
    filter(yearl == i_year, monl >= i_month, circuit == i_circuit, yeara!=yearl) %>%
    pull(JCS)
  
  i_jcs_d <- jcs_circuit_month %>%
    filter(yeara == i_year, yearl == i_year, mona <= i_month, monl >= i_month, circuit == i_circuit) %>%
    pull(JCS)

  i_jcs <- c(i_jcs_a, i_jcs_b, i_jcs_c, i_jcs_d)
  
  if(length(i_jcs) > 3) {
    panel_combn <- combn(i_jcs, 3) %>% apply(2, median)
    mean_panel[i] <- panel_combn %>% mean()
  } else if(length(i_jcs) %in% c(1:3)) {
    mean_panel[i] <- i_jcs %>% median()
  }
  
  mean_all[i] <- i_jcs %>% mean()
  median_all[i] <- i_jcs %>% median()
  
  if(i %% 100 == 0) print(round(i/length(mean_panel) * 100, 1))
}

my_tibble <- my_tibble %>%
  mutate(mean_panel = mean_panel,
         mean_all = mean_all,
         median_all = median_all)

write_rds(my_tibble, "panel_means.rds")
